package patrol.mobile.com.mobilepatrol.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import patrol.mobile.com.mobilepatrol.DBHelper;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.route_query.RouteType;
import patrol.mobile.com.mobilepatrol.activity.route_query.SubOffice;
import patrol.mobile.com.mobilepatrol.activity.route_query.SubOfficeAdapter;
import patrol.mobile.com.mobilepatrol.activity.route_query.TotalRouteActivity;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.StringUtils;

/**
 * Created by apple on 18/1/24.
 */

public class SubOfficeListFragment extends BaseFragment {
    @BindView(R.id.recycler)
    RecyclerView recycler;


    @BindView(R.id.tv_org)
    TextView tv_org;

    @BindView(R.id.tv_mail)
    TextView tv_mail;


    DBHelper db;
    private SubOfficeAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.route_list;
    }
    List<SubOffice> subOffices = new ArrayList<>();
    @Override
    protected void initData() {
        db = new DBHelper(getActivity());
        tv_org.setText(UserManager.getUser().bmname);
        tv_mail.setText("共"+String.format("%.2f", db.getRouteManageMiles())+"km");
        //获取所有分局
        List<String> subofficeNames = db.getRouteManageSubofficeNames();
        if (subofficeNames.size()==0){
            showToast("请初始化路线查询单位信息");
            return;
        }
        SubOffice subOffice;
        for (int i=0;i<subofficeNames.size();i++){
            subOffice = new SubOffice();
            //获取每个分局下的所有公路类型
            List<String> routetypes = StringUtils.sortList(db.getRouteManageTypes(subofficeNames.get(i)));

            List<RouteType> types = db.getTypes(routetypes,subofficeNames.get(i));
            subOffice.orgName = subofficeNames.get(i);
            subOffice.routeTypes = types;
            subOffices.add(subOffice);
        }
        mAdapter = new SubOfficeAdapter((TotalRouteActivity) getActivity(),subOffices);
        recycler.setAdapter(mAdapter);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initView() {
        super.initView();
        recycler.setLayoutManager(new LinearLayoutManager(getActivity()));

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        db.close();
    }
}
